
//check if page contains video tag.
block = false;
pause = false;

window.setTimeout(function(){
	var _videos = document.getElementsByTagName("video");
	for(var i = 0, _video; _video = _videos[i]; i++){
		_video.addEventListener('playing', handler, false);
		_video.addEventListener('pause', pausehandler, false);
	}
},0);

function handler(){ 
	if(!block || !pause){
	  console.log("Inside Playing Handler");
	  chrome.extension.sendRequest({}, function(response) {});
	  block = true;
	  pause = true;
	  this.play();
	  console.log("ready to go out");
    }
	else
	{
		block = false;
		pause = false;
	}
}

function pausehandler(){
	pause = true;
}

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
 var videos = document.getElementsByTagName("video");
 if (request.action == "pauseVideo")
  for (var i = 0, video; video = videos[i]; i++)
	video.pause();
 else {
  //no action necessary here!
 }
 sendResponse({}); // Send nothing..
});
